package com.yihaodian.search.broker;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.yihaodian.common.thread.NamedThreadFactory;

public class ThreadPools {

	public static final String COMMON = "common";
	
	public static final String INDEX = "index";
	
	public static final String SEARCH = "search";
	
	private static final int cores = Runtime.getRuntime().availableProcessors();
	
	private static final ExecutorService indexExecutor= new ThreadPoolExecutor(cores + 1, cores * 2, 60,
			TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(
					cores * 1000),new NamedThreadFactory("index-executor"));
	
	private static final ExecutorService searchExecutor= new ThreadPoolExecutor(cores + 1, cores * 2, 60,
			TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(
					cores * 1000),new NamedThreadFactory("search-executor")); 
	
	private static final ExecutorService commonExecutor= new ThreadPoolExecutor(cores + 1, cores * 2, 60,
			TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(
					cores * 1000),new NamedThreadFactory("common-executor")); 

	public static ExecutorService getThreadPool(String executorName) {
		if(executorName==INDEX){
			return indexExecutor;
		}else if(executorName==SEARCH){
			return searchExecutor;
		}
		return commonExecutor;
	}

}
